package com.epoch.server.kernel.utils;

import java.sql.*;
import java.util.ResourceBundle;

public class DBUtil {
    private static final String CONFIG_FILE = "jdbc";
    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle(CONFIG_FILE);
    private static final String URL = BUNDLE.getString("url");
    private static final String USERNAME = BUNDLE.getString("username");
    private static final String PASSWORD = BUNDLE.getString("password");
    private static final String DRIVER_NAME = BUNDLE.getString("driver_name");


    static {
        try {
            Class.forName(DRIVER_NAME);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnectDB() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
